Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have the following javascript file for which I am trying to develop a script to make online changes. The problem I can't follow is that when I do the following lines:
open FILE, "$js" or die $!; flock (FILE, 1) or die "Can't lock dev.js for reading"; my @all = <FILE>; close FILE;
The whole file gets slurped up into $all[0] which prevents me from looping through the array to find the lines I need.

I could presumably split the scalar on new lines, but since I don't understand the reason for it not being split up already I thought I'd hightail it over here and ask.

Thanks for any help given

function init() { menus[0] = new menu(138, "vertical", 14, 132, -2, -2, "#000099", " +#000099", "Arial", 9, "bold", "bold", "#FFCC00", "white", 1, "gray", 2, "rollover:tr +i.gif:tri.gif", false, true, true, true, 12, true, 4, 4, "black"); menus[0].addItem("", "", 0, "left", "", 0); menus[0].addItem("", "", 22, "left", "", 1); menus[0].addItem("", "", 22, "left", "", 2); menus[0].addItem("", "", 22, "left", "", 3); //Sub Menu for 2nd Main Menu Item ("web building"): menus[1] = new menu(165, "vertical", 0, 0, -5, -5, "#000099", "#00 +0099", "Arial", 9, "bold", "bold", "#FFCC00", "white", 1, "gray", 2, 62, false, true, fal +se, true, 6, true, 4, 4, "black"); menus[1].addItem("banda.htm", "", 22, "left", "100k - 150k", 0); menus[1].addItem("bandb.htm", "", 22, "left", "150k - 200k", 0); menus[1].addItem("bandc.htm", "", 22, "left", "over 200k", 0); menus[1].addItem("#", "", 22, "left", "", 4); menus[1].addItem("prty.htm", "NEWWIN", 22, "left", "", 0); //Sub Menu for 3rd Main Menu Item ("News"): menus[2] = new menu(165, "vertical", 0, 0, 0, 0, "#000099", "#0000 +99", "Arial", 9, "bold", "bold", "#FFCC00", "white", 1, "gray", 2, "r +ollover:tri.gif:tri.gif", false, true, false, false, 0, true, 4, 4, " +black"); menus[2].addItem("rices.htm", "", 22, "left", "All Prices", 0); //menus[2].addItem("#", "", 22, "left", "", 5); //Sub Menu for 4th Main Menu Item ("Search"): menus[3] = new menu(165, "vertical", 0, 0, 0, 0, "#000099", "#0000 +99", "Arial", 9, "bold", "bold", "#FFCC00", "white", 1, "gray", 2, "> +>", false, true, false, false, 0, true, 4, 4, "black"); menus[3].addItem("ntro.htm", "", 22, "left", "Introduction", 0); menus[3].addItem("rices.htm", "", 22, "left", "rices", 0); //menus[3].addItem("#", "", 22, "left", "", 6); //Sub Menu for Sub Menu "Sports News": menus[4] = new menu(135, "vertical", 0, 0, 0, 0, "#000099", "#0000 +99", "Arial", 9, "bold", "bold", "#FFCC00", "white", 1, "gray", 2, 6 +2, false, true, false, false, 0, true, 4, 4, "black"); menus[4].addItem("ills.htm", "", 22, "left", "ills", 0); //Sub Menu for Sub Menu "Sports News": menus[5] = new menu(135, "vertical", 0, 0, 0, 0, "#000099", "#0000 +99", "Arial", 9, "bold", "bold", "#FFCC00", "white", 1, "gray", 2, 6 +2, false, true, false, false, 0, true, 4, 4, "black"); menus[5].addItem("ills.htm", "", 22, "left", "ills", 0); //Sub Menu for Sub Menu "Sports News": menus[6] = new menu(135, "vertical", 0, 0, 0, 0, "#000099", "#0000 +99", "Arial", 9, "bold", "bold", "#FFCC00", "white", 1, "gray", 2, 6 +2, false, true, false, false, 0, true, 4, 4, "black"); menus[6].addItem("ills.htm", "", 22, "left", "ills", 0); }

2005-03-16 Janitored by Arunbear - added readmore tags, as per Monastery guidelines

Replies are listed 'Best First'.
Re: file not being split at new lines
by dragonchild (Archbishop) on Mar 16, 2005 at 14:50 UTC
    Was the JS file last edited on the same machine you're writing a script to edit it on? You might have a different line-ending. Windows uses \n\r, Unix uses \n, and Mac uses \r. You might have to set $\ to the right value for it to automatically split the incoming data.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

      \r\n, not \n\r, and only classic Macs use \r IIRC.
Re: file not being split at new lines
by Roy Johnson (Monsignor) on Mar 16, 2005 at 14:53 UTC
    My guess is that your Javascript file does not have the line endings that Perl thinks are normal for your system. What is the value of $\ (output it as hex using unpack 'H*', $\)?

    Caution: Contents may have been coded under pressure.
Re: file not being split at new lines
by sh1tn (Priest) on Mar 16, 2005 at 16:37 UTC
    while(<FILE>){ push @all, $_ for split /\r?\n/ }