> -----Original Message-----
> From: perl-win32-users-boun...@listserv.activestate.com [mailto:perl-
> win32-users-boun...@listserv.activestate.com] On Behalf Of Greg Aiken
> Sent: 10 August 2012 01:22
> To: Perl-Win32-Users@listserv.activestate.com
> Subject: trouble understanding/using file handle in a sub routine
>
> i am using IO::File so that i can use the seekable feature to set the
> file pointer position.
>
>    $OUT_BINARY = new IO::File "> test.file";
>    binmode ($OUT_BINARY)
>
> if i execute this, the write works:
>
>    print $OUT_BINARY $data;
>
> what i want to do (for the sake of pointing out the problem) is to
> instead turn the print statement into a subroutine - where i would call
> it passing in $OUT_BINARY (the file handle) and $data (the data to
> write).
>
> the sub looks like this:
>
>    sub write_fp_data {
>
>       #called with:
>       #   file pointer = $_[0]
>       #   data value = $_[1]
>
>       #this attempts to do the write - but fails
>       print $_[0] $_[1];                    #line 101
>    }
>
> and the call to the sub, looks like this:
>
>    &write_fp_data($OUT_BINARY, $data);
>
> this all seems 'simple enough', though when using this with the sub
> bombs with:
>
> Scalar found where operator expected at FILE2.PL line 101, near "] $_"
>         (Missing operator before  $_?)
> syntax error at FILE2.PL line 101, near "] $_"
> Execution of FILE2.PL aborted due to compilation errors.
>
> im certain this must have something to do with my using $_[0] versus
> $OUT_BINARY in the sub routine - but i cant see what is wrong with this
> code.  would be most appreciative if anyone can see the glaring
> problem!

Perl is misinterpreting your print statement. See 'perldoc -f print' for some 
hints on making your intentions clear.

Note that you don't need IO::File in order to seek. The file descriptor 
returned by open can be positioned by the seek function just fine. In fact, the 
documentation for IO::File says that the single argument constructor is just a 
front tend for open.

Also, hopefully your function does more than just the print statement, 
otherwise there is no point to it. In which case, it might be better to assign 
the parameters to local variables with appropriate names, rather than relying 
on a comment. For example...

sub write_fp_data {
    my $file_pointer = shift;
    my $data_value = shift;
    print $file_pointer $data_value;
}

HTH


--
Brian Raven




Please consider the environment before printing this e-mail.

This e-mail may contain confidential and/or privileged information. If you are 
not the intended recipient or have received this e-mail in error, please advise 
the sender immediately by reply e-mail and delete this message and any 
attachments without retaining a copy.

Any unauthorised copying, disclosure or distribution of the material in this 
e-mail is strictly forbidden.
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to