restriction on file handle

Discussion in 'Perl Misc' started by Yan Wu, Jan 21, 2004.

  1. Yan Wu

    Yan Wu Guest

    HI,
    In the below script, "info1" is never defined but it could still run
    correctly.
    Is there any way to restrict the use of undefined file handle?
    Thanks.
    Yan


    #!/p/perl/bin/perl -w
    #
    # Program to open the password file, read it in,
    # print it, and close it again.
    use strict;
    use FileHandle;

    my $file;
    my @lines;


    $file = 'myfile'; # Name the file
    open(info1, $file); # Open the file
    @lines = <info1>; # Read it into an array
    close(info1); # Close the file
    print @lines; # Print the array
     
    Yan Wu, Jan 21, 2004
    #1
    1. Advertising

  2. In article <>,
    Yan Wu <> wrote:
    : In the below script, "info1" is never defined but it could still run
    :correctly.
    : Is there any way to restrict the use of undefined file handle?

    I don't understand?

    :eek:pen(info1, $file); # Open the file

    You define it right there.

    Perhaps you meant to write about:

    open($info1, $file) or die "$file could not be opened";
    @lines = <$info1>;
    close($info1);

    If so then your 'use strict' would complain.
    --
    So you found your solution
    What will be your last contribution?
    -- Supertramp (Fool's Overture)
     
    Walter Roberson, Jan 21, 2004
    #2
    1. Advertising

  3. Yan Wu <> wrote:

    Purdue, eh? I bet spaf is a good teacher.

    > HI,
    > In the below script, "info1" is never defined but it could still run
    > correctly.


    Sure it is. You define it when you open the file.

    > Is there any way to restrict the use of undefined file handle?


    Check to see if the open is successful. (see below)

    > Thanks.
    > Yan
    >
    >
    > #!/p/perl/bin/perl -w


    For recent versions of Perl you can use the warnings pragma for more
    control (if you need it).

    use warnings;

    instead of -w.

    > #
    > # Program to open the password file, read it in,
    > # print it, and close it again.
    > use strict;


    Strictures in place. Good.

    > use FileHandle;


    What's this for? You don't use it anywhere.

    >
    > my $file;
    > my @lines;
    >
    >
    > $file = 'myfile'; # Name the file


    You can initialize this at the same time you declare it (if you want):

    my $file = 'myfile';


    > open(info1, $file); # Open the file


    You should *always* check to see if an open() succeeded. The convention is
    also to use capital letters for filehandles; it's not technically necessary
    but will make it easier for other people to read your code.

    open INFO1, $file or die "Cannot open $file: $!";

    > @lines = <info1>; # Read it into an array


    There's nothing wrong with slurping in "small" files, but if you suspect
    the file could be "large" you might want to process it line-by-line. These
    are relative terms; they depend on what you're doing and the resources
    available.

    > close(info1); # Close the file
    > print @lines; # Print the array


    Since all you're doing is printing the file, you don't really even need
    that @lines array. For example,

    print <INFO1>;

    will print the contents of the file associated with the INFO1 filehandle.

    You also don't have to comment every single line of code. Comments are for
    explaining *why* some code exists, or to give a high-level explanation of
    what's going on. That is, they're notes to yourself or future maintenance
    programmers to make fixing or modifying the code easier.

    --
    David Wall
     
    David K. Wall, Jan 21, 2004
    #3
  4. Yan Wu

    Ben Morrow Guest

    Yan Wu <> wrote:
    > HI,
    > In the below script, "info1" is never defined but it could still run
    > correctly.


    What do you mean by 'is never defined'? The filehandle info1 is
    created by the open statement.

    > Is there any way to restrict the use of undefined file handle?


    This will only allow lexical filehandles:

    use strict;
    use subs qw/open/; # to override the builtin

    sub open (\$;$@) {
    # it is necessary to give the first arg separately
    # as CORE::eek:pen has a prototype of (*;$@)
    my ($fh, $first, @rest) = @_;
    CORE::eek:pen $$fh, $first, @rest;
    }

    .. This means that these

    open my $info1, $file;

    my $info1;
    open $info1, $file;

    will succeed whereas these

    open info1, $file;
    open *info1, $file;
    open \*info1, $file;
    open "info1", $file;

    will fail at compile time.

    Ben

    --
    Joy and Woe are woven fine,
    A Clothing for the Soul divine William Blake
    Under every grief and pine 'Auguries of Innocence'
    Runs a joy with silken twine.
     
    Ben Morrow, Jan 21, 2004
    #4
  5. Yan Wu

    Ben Morrow Guest

    Ben Morrow <> wrote:
    >
    > This will only allow lexical filehandles:


    OK, there's a case I missed:

    my $fh = \*FH;
    open $fh, "file";

    and

    my $fh = *FH;
    open $fh, "file";

    will succeed despite using a global FH. That's pretty obscure,
    though...

    Ben

    --
    $.=1;*g=sub{print@_};sub r($$\$){my($w,$x,$y)=@_;for(keys%$x){/main/&&next;*p=$
    $x{$_};/(\w)::$/&&(r($w.$1,$x.$_,$y),next);$y eq\$p&&&g("$w$_")}};sub t{for(@_)
    {$f&&($_||&g(" "));$f=1;r"","::",$_;$_&&&g(chr(0012))}};t #
    $J::u::s::t, $a::n::eek:::t::h::e::r, $P::e::r::l, $h::a::c::k::e::r, $.
     
    Ben Morrow, Jan 21, 2004
    #5
  6. Yan Wu

    Yan Wu Guest

    Thanks a lot. This is what I want.
    Yan

    Ben Morrow wrote:
    -
    - use strict;
    - use subs qw/open/; # to override the builti
     
    Yan Wu, Jan 21, 2004
    #6
  7. Also sprach Ben Morrow:

    > Ben Morrow <> wrote:
    >>
    >> This will only allow lexical filehandles:

    >
    > OK, there's a case I missed:
    >
    > my $fh = \*FH;
    > open $fh, "file";
    >
    > and
    >
    > my $fh = *FH;
    > open $fh, "file";
    >
    > will succeed despite using a global FH. That's pretty obscure,
    > though...


    Not really. A filehandle in Perl is always a GLOB.

    open my $fh, ...;

    is just syntactic sugar and perl will internally generate a GLOB and
    store a reference to it in $fh.

    Tassilo
    --
    $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
    pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
    $_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval
     
    Tassilo v. Parseval, Jan 21, 2004
    #7
  8. Yan Wu

    Anno Siegel Guest

    Yan Wu <> wrote in comp.lang.perl.misc:
    > Thanks a lot. This is what I want.
    > Yan
    >
    > Ben Morrow wrote:
    > -
    > - use strict;
    > - use subs qw/open/; # to override the builti


    That's a pretty radical approach.

    use warnings FATAL => 'closed';

    in an appropriate scope is less intrusive.

    Anno
     
    Anno Siegel, Jan 22, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dietrich
    Replies:
    1
    Views:
    656
    Joe Smith
    Jul 22, 2004
  2. =?Utf-8?B?RWQ=?=

    file restriction - Forms authentication

    =?Utf-8?B?RWQ=?=, May 31, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    382
    Jim Cheshire [MSFT]
    Jun 1, 2004
  3. z f
    Replies:
    2
    Views:
    5,100
  4. Leon
    Replies:
    2
    Views:
    542
  5. =?ISO-8859-1?Q?KLEIN_St=E9phane?=
    Replies:
    3
    Views:
    461
    hanumizzle
    Oct 6, 2006
Loading...

Share This Page