开发者

Weird behavior with Perl string concatenation

I'm working on a pretty simple script, reading a maplist.txt file and using the \n separated map 开发者_JAVA百科names in it to build a command string - however, I'm getting some unexpected behavior.

My full code:

# compiles a map pack from maplist.txt
# for every server.
# Filipe Dobreira <dobreira@gmail.com>
# v1 @ Sept. 2011

use strict;
my @servers = <*>;
foreach my $server (@servers)
{
        # we only want folders:
        next if -f $server;
        print "server: $server\n";

        my $maplist = $server . '/orangebox/cstrike/maplist.txt';
        my $mapdir  = $server . '/orangebox/cstrike/maps';

        print "   maplist: $maplist\n";
        print "   map folder: $mapdir\n";

        # check if the maplist actually exists:
        if(!(-e $maplist))
        {
                print "!!! failed to find $maplist\n";
                next;
        }

        open MAPLIST, "<$maplist";
        foreach my $map (<MAPLIST>)
        {
                chomp($map);
                next if !$map;

                # full path to the map file:
                my $mapfile = "$mapdir/$map.bsp";
                print "$mapfile\n";
        }
}

Where I declare $mapfile, I expect the result to be something like:

zombieescape1/orangebox/cstrike/maps/ze_stargate_escape_v8.bsp

However, it seems like the concatenation is being made to the START of the string, and the final result ends up being something like:

.bspiescape1/orangebox/cstrike/maps/ze_stargate_escape_v8

So the .bsp portion is actually being written over the start of the leftmost string. I have very little perl experience, and I can only assume this is me failing to understand some quirk or operator behavior.

Note: I've also tried using "${mapdir}/${map}.bsp", concatenating everything with the dot operator, and a join "", $mapdir, $map, ".bsp", with the same result.

Thanks in advance.

PS: for reference, here's what a maplist.txtlooks like:

zm_3dubka_v3
zm_4way_tunnel_v2
zm_abstractchode_pyramid2
zm_anotheruglyzmap_v1e
zm_app7e_betterbworld_JDfix_v3
zm_atix_helicopter_mini
zm_base_winter_beta3
zm_battleforce_panic_ua
zm_black_lion_macd_v8
zm_bunker_f57_v2
zm_burbsdelchode_b3
zm_choddarena_b12
zm_choddasnowpanic_b4
zm_citylife_V2b
zm_crazycity
zm_deep_thought_nv
zm_desert_fortress_v2
ZM_desprerados_a1
zm_doomlike_station_v2
zm_dust_arena_v1_final
zm_exhibit_night_2F
zm_facility_v1
zm_farm3_nav72
zm_firewall_samarkand
zm_fortress_b7
zm_ghs_flats
zm_gl33m4x_errata
zm_idm_hauntedhouse_v1
zm_industry_v2
zm_kruma_kakariko_village_006
zm_kruma_panic_004
zm_lila_off!ce_v4
zm_little_city_v5pf_fix
zm_moonlight_v3_pF
zm_moon_roflicious_pF_02
zm_moocbblechode_b2
zm_mountain_b2
zm_neko_abura_v2
zm_neko_athletic_park_v2
zm_novum_v3_JDfix
zm_ocx_orly_v4
zm_officeattack_b5a
zm_officerush_betav7
zm_officesspace_pfss
zm_omi_facility_pfv2
zm_penumbra_PF3
zm_raindance_ak_v2
zm_roflicious_pfcf2
zm_roy_abandoned_canals_new
zm_roy_barricade_factory
zm_roy_highway
zm_roy_industrial_complex
zm_roy_old_industrial_pF
zm_roy_the_ship_pf
zm_roy_zombieranch_night_b4
zm_survival_f2a
zm_temple_v3pf
zm_towers_v3
zm_tx_highschool_zkedit_v2
zm_unpanicv2_pF
zm_vc2_office_redone_b1
zm_wasteyard_beta3
zm_winterfun_b4a
zm_wtfhax_v6
zm_wtfhax_v6e
zm_wwt_twinsteel_v8


I'd guess that the maplist.txt has non-unix line endings - probably dos - and as result you see what looks like prepending.

The problem is that the chomp() is only consuming one of the two line ending characters, leaving the carriage return behind.

You might find that if you set the Perl special variable $/ (input record seperator) before opening the map list, that chomp then does the job - it will consume both line-ending characters.

$/ = qq{\r\n};

Another solution would be to convert the line endings in the file before processing, perhaps using dos2unix.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜