<8----- package mypack; use Data::Dumper; sub wrstr { print Dumper(\@_); my ($fd, $str) = @_; print $fd "$str\n"; } 1; package main; use Data::Dumper; sub wrstr { print Dumper(\@_); my ($fd, $str) = @_; print $fd "$str\n"; } open(FD,">","dummy"); wrstr(FD,"12345"); mypack::wrstr(FD,"12345"); close FD; __END__ $VAR1 = [ 'FD', '12345' ]; $VAR1 = [ 'FD', '12345' ]; While it seems that you are passing file handles to the wrstr functions, perl is actually passing the *string* "FD" to those functions. In the main package this isn't a problem for perl; perl treats the string FD as a symbolic reference. Since there is a file handle in that package (file handles are local to the package in which they are declared) perl is able to resolve the reference. In the 'mypack' package, however, there is no FD handle, so perl cannot resolve it to a file handle. Since warnings are not turned on, perl fails quietly. You can fix the problem by using a variable to hold a _reference_ to the file handle: open(my $fd, ">", "dummy"); wrstr($fd, "12345"); mypack::wrstr($fd, "12345"); close $fd; This is arguably the better approach anyway because it ties the file handle to a lexical variable. When the variable goes out of scope, the reference count on the file handle goes to zero, so the file handle is closed automatically. Randy. [allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => <40F63769.4060102@thepierianspring.org> [ref] => <20040715091348.0000431f.cialdi@firenze.net> [htmlstate] => on_nl2br [postusername] => Randy [ip] => ml-perl@thepier [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 2 [islastshown] => [isfirstshown] => [attachments] => [allattachments] => ) --> passing files to functions - PERL Beginners
Professional Web Applications Themes

passing files to functions - PERL Beginners

I need to pass a file descriptor to a function. If this function is defined in package it doesn't work! I attach a little example. Why it doesn't works, and how can I make it works? thanks -- Massimiliano Cialdi net it...

  1. #1

    Default passing files to functions

    I need to pass a file descriptor to a function.
    If this function is defined in package it doesn't work!

    I attach a little example.

    Why it doesn't works, and how can I make it works?

    thanks
    --
    Massimiliano Cialdi
    net
    it

    Massimiliano Guest

  2. #2

    Default Re: passing files to functions

    Massimiliano Cialdi wrote: 

    use strict;
    use warnings;

    provides a clue:

    Bareword "FD" not allowed while "strict subs" in use at main.pl line 14.
    Bareword "FD" not allowed while "strict subs" in use at main.pl line 15.
    Execution of main.pl aborted due to compilation errors.

    Another clue is provided by using Data::Dumper:

    -----><8-----
    package mypack;

    use Data::Dumper;

    sub wrstr {
    print Dumper(\_);
    my ($fd, $str) = _;
    print $fd "$str\n";
    }

    1;

    package main;

    use Data::Dumper;

    sub wrstr {
    print Dumper(\_);
    my ($fd, $str) = _;
    print $fd "$str\n";
    }

    open(FD,">","dummy");

    wrstr(FD,"12345");
    mypack::wrstr(FD,"12345");

    close FD;

    __END__

    $VAR1 = [
    'FD',
    '12345'
    ];
    $VAR1 = [
    'FD',
    '12345'
    ];

    While it seems that you are passing file handles to the wrstr functions,
    perl is actually passing the *string* "FD" to those functions. In the
    main package this isn't a problem for perl; perl treats the string FD as
    a symbolic reference. Since there is a file handle in that package (file
    handles are local to the package in which they are declared) perl is
    able to resolve the reference. In the 'mypack' package, however, there
    is no FD handle, so perl cannot resolve it to a file handle. Since
    warnings are not turned on, perl fails quietly.

    You can fix the problem by using a variable to hold a _reference_ to the
    file handle:

    open(my $fd, ">", "dummy");
    wrstr($fd, "12345");
    mypack::wrstr($fd, "12345");
    close $fd;

    This is arguably the better approach anyway because it ties the file
    handle to a lexical variable. When the variable goes out of scope, the
    reference count on the file handle goes to zero, so the file handle is
    closed automatically.

    Randy.
    Randy Guest

  3. #3

    Default Re: passing files to functions

    On Thursday 15 July 2004 00:13, Massimiliano Cialdi wrote: 

    That is a Frequently Asked Question and can be found in perlfaq5.

    perldoc -q "How do I pass filehandles between subroutines"

    And:

    perldoc -q "How can I use a filehandle indirectly"


    And also see the section "Typeglobs and Filehandles" in perldata.

    perldoc perldata


    John
    --
    use Perl;
    program
    fulfillment
    John Guest

Similar Threads

  1. Passing variables between functions
    By pope on acid in forum Macromedia Flex General Discussion
    Replies: 2
    Last Post: May 15th, 12:25 AM
  2. Passing vars into functions
    By jaquito13 in forum PHP Development
    Replies: 2
    Last Post: May 18th, 09:06 PM
  3. passing arguments to functions
    By jassismara@xtra.co.nz in forum PERL Beginners
    Replies: 1
    Last Post: January 20th, 01:32 AM
  4. Passing arrays to functions
    By Eric Kincl in forum PHP Development
    Replies: 1
    Last Post: November 19th, 09:13 PM
  5. Passing arrays through functions
    By Henry in forum ASP
    Replies: 3
    Last Post: September 15th, 02:44 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139