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

Hi Perlmonks
I want to ask what does $/ = "\/\/\n"; means?? Esp the $/, does it means that we create a scalar var called"/"? if it's so... don't we have to use double // instead of /? May be it's too easy for all of you,I'm just learning perl a few days ago.......:p
Thanks for the help.

Replies are listed 'Best First'.
Re: What does $/ = "\/\/\n"; mean?
by gmax (Abbot) on Jan 11, 2002 at 15:52 UTC
    Consider the following code.
    #/usr/bin/perl -w use strict; { local $/ = "\/\/\n"; while (<DATA>) { #chomp; print "< $_ > \n"; } } __DATA__ first row second row // another first another second // one more (3.1) one more (3.2) //
    As jeffa mentioned, the separator now will look for this sequence of characters (//\n) as a record separator. During the while loop, instead of reading one line, it will read up to this special separator.
    The output will be:
    < first row second row // > < another first another second // > < one more (3.1) one more (3.2) // >
    You can easily see where each record begins and ends.
    However, there is an additional effect of setting the $/ variable. It will also affect the behaviour of the chomp function.
    Try it. Uncomment the line with the chomp function, and you will get this output instead:
    < first row second row > < another first another second > < one more (3.1) one more (3.2) >
    The entire separating sequence will be chopped out, but the inner newlines will stay untouched, because they were not separators. Actually, they are part of the string, so the chomp function does not affect them.
    HTH

    update It's a good policy to use local with $/ to avoid side effects if your script is dealing with other input as well.
    _ _ _ _ (_|| | |(_|>< _|
      Excellent demonstration (++) but it is misleading in one minor way.... Your example data is actually separated by "\n//\n", which is a bit different from the original question about the separator "//\n". Your example makes it sound like the data below consists of two records, when it is actually four.
      __DATA__ first row// second row // another first// another second //

      -Blake

        Thanks for your comment. I agree on the VISUAL misleading of the data. However, even though the data looks like it's divided by "\n//\n", the code is actually separating by "//\n" only, leaving one extra "\n" in the data after chomping.
        I presented the data that way because I has in mind fortunes files, where records are separated by a "\n%%\n".
        _ _ _ _ (_|| | |(_|>< _|
(jeffa) Re: wanna ask
by jeffa (Bishop) on Jan 11, 2002 at 08:48 UTC
    $/ is the input record separator. As dmmiller2k said, you can read all about it at the docs.

    But what does setting it to THAT value do?

    Only the person that wrote it really knows the answer to that question. Taking a quess, i would say that they were reading in a file and using the delimiter "//\n" to 'break it up' in an uncommon place. The most common place to 'break up' a file is after each newline.

    You will probably never need to do this or even worry about it. Especially if you are new to Perl.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    
      You will probably never need to do this or even worry about it. Especially if you are new to Perl.
      Heh, just an interesting side note. One of my first Perl projects was to modify prewritten scripts used for Radius time accounting. The data munging was all based on $\ = '', $PREMATCH and $POSTMATCH. Not a pleasant introduction to Perl. Fortunately, I had a camel. Unfortunately, the monastery didn't exist yet.

      --Jim

Re: wanna ask
by dmmiller2k (Chaplain) on Jan 11, 2002 at 08:00 UTC

    $/ is one of Perl's special variables. Which one? Take a look at perlman:perlvar.

    dmm

    You can give a man a fish and feed him for a day ...
    Or, you can
    teach him to fish and feed him for a lifetime