in reply to Re^3: What is the right way of concatenating strings
in thread What is the right way of concatenating strings

yummmi, I really like that slurping concept. Just one line code..perl is amazing, Thanx again man. Can you pl explain me what do mean by three parameter here->> " It is strongly recommended that you use a three parameter open, especially when you obtain the name of the file from the user (as in this case) " and also throgh some light on things (how this is working)inside the curlies on right hand side...please
my $input = do {local %/; <AA>;};
waiting...

Replies are listed 'Best First'.
Re^5: What is the right way of concatenating strings
by Hue-Bond (Priest) on May 02, 2006 at 13:53 UTC

    Many functions in Perl can be called with a variable number of arguments. For example, split:

    split /PATTERN/,EXPR,LIMIT split /PATTERN/,EXPR split /PATTERN/

    In this case, Perl will assume some default values for the arguments you don't specify. In the case of open, you can do:

    open FILEHANDLE,EXPR open FILEHANDLE,MODE,EXPR open FILEHANDLE,MODE,EXPR,LIST open FILEHANDLE,MODE,REFERENCE open FILEHANDLE

    As you can see, there's more than one way to do it :^). You're being advised to use the three argument form of open, which normally reads like this:

    open $fd, '<', 'input_file'    or die "open: $!";    ## <-- you should always check for errors.
    my $input = do {local %/; <AA>;};

    As for this, first take a look to Coping with scoping to understand what local means.

    --
    David Serrano

Re^5: What is the right way of concatenating strings
by GrandFather (Saint) on May 02, 2006 at 20:25 UTC

    Open comes in a number of flavours. The two parameter open combines the open mode and file name in the second parameter. The three parameter open seperates the mode (second parameter) from the file name (third parameter). If you use the two parameter open and especially if you don't specify the mode because you want input, a user supplied file name that starts with '<', '>' or any of the other mode characters will set the file mode. That can include doing some pretty interesting stuff like forking a child process!

    Now for that Perl magic:

    my $input = do {local $/; <AA>;};

    The do lets you put a block of statements where an expression is allowed. The result of the do block is the result of the last statement evaluated.

    The special variable $/ stores the input line seperator. By default this is whatever line end sequence is native on your system. By declaring it local we save the old value it had and set the value to undef. With $/ set to undef the <AA> reads the whole file - there is no line end seperator to match. <AA> is the last statement evaluated in the do block so its result is assigned to $input.

    Remember though that this reads the whole file in to memory. If the file is small (say up to a few megs) that is no big deal. If the file is gigabytes in size then it can be a real problem. It's a neat thing to know about and use, but you have to suit it to the application.

    Update: s|%/|$/|g


    DWIM is Perl's answer to Gödel