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

What gets affected if I modify this variable to something arbitrary? examples for my better understanding?

Replies are listed 'Best First'.
Re: $/ variable
by reyjrar (Hermit) on Aug 21, 2001 at 21:15 UTC
    Well, $/ is the input record seperator, its what things like <> use to determine when to iterate to the next record. Default is "\n". some examples:

    A) echo 'blah;hi;blah;' |perl -e 'while(<>) { print "$_\n"; }' vs. B) echo 'blah;hi;blah;' |perl -e '$/=";";while(<>) { print "$_\n"; }'
    A outputs:
    blah;hi;blah;

    B outputs:
    blah;
    hi;
    blah;

    If you're interessted in changing this value, you might also look at split() which is generally safer to use (especially if you're not using strict and warnings).

    -brad..
Re: $/ variable
by Hanamaki (Chaplain) on Aug 21, 2001 at 21:34 UTC
Re: $/ variable
by jryan (Vicar) on Aug 21, 2001 at 21:21 UTC
    $/ is the input record seperator (default is "\n"), and if you change it to something else (such as "a" or "\n\n"), perl changes where it seperates "lines" on a file. For instance, here is a script that will read a file on paragraphs (defined as "\n\n") rather than newlines, and print !!!!!PARAGRAPH!!!!! before each one, to let you know that its working:
    #!/usr/bin/perl -w use strict; open (FILE, "input.txt"); while (<FILE>) { print "\n!!!!!PARAGRAPH!!!!!\n", $_; }
    Here is a sample text file to try it on: (because im nice :))
    dstgs dahfgvsd kjghkls djgklsdjgl ksdjglk;j sdkl;ghsd jkljg lkds;j glk +;dsjglk; sdjgkldsjgl ;ksdjgkl;d sjglk;s djgkjdskgjdsjg lkdsjglk;sdjg +l;ksdjglkd sjglkdsjlg sd glsd dstgs dahfgvsd kjghkls djgklsdjgl ksdjglk;j sdkl;ghsd jkljg lkds;j glk +;dsjglk; sdjgkldsjgl ;ksdjgkl;d sjglk;s djgkjdskgjdsjg lkdsjglk;sdjg +l;ksdjglkd sjglkdsjlg sd glsd dstgs dahfgvsd kjghkls djgklsdjgl ksdjglk;j sdkl;ghsd jkljg lkds;j glk +;dsjglk; sdjgkldsjgl ;ksdjgkl;d sjglk;s djgkjdskgjdsjg lkdsjglk;sdjg +l;ksdjglkd sjglkdsjlg sd glsd
      where did you modify $/ in your script? Whether I cant see it or I cant follow your great art of obfuscation. :-)
      So just for readability I'd suggest to add a line to play with
      # (un)comment or modify one of the following #undef $/; #tread all as one line $/ = "d"; # whatever you want it to be
      For cool examples on how to use that take a look at Obfuscation. Well sometimes its not only cool but also clever.

      Have a nice day
      All decision is left to your taste
Re: $/ variable
by little (Curate) on Aug 21, 2001 at 21:14 UTC