http://www.perlmonks.org?node_id=52983

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

The following shows my first dabblings into File::Temp, which are essentially pulled directly from it's POD.

The first 2 functions, WERX and WERX2, will successfully create their files in /tmp/.   The second 2 funtions, DUNWERK and DUNWERK2, are supposed to also unlink their created file, but they don't work.

Far as I can tell DUNWERK does not create a file, and DUNWERK2 bombs with the following error:
undefined subroutine &main::unlink0 called at "this_script" line "unlink0..." <STDIN> chunk2.

What am I doing wrong here?
    cheers,
    Don
    striving for Perl Adept
    (it's pronounced "why-bick")

#!/usr/bin/perl -w # filetemptest.pl # 2001-01-18 # File::Temp 0.11 Perl 5.00503 # File::Spec 0.82 Debian 2.2 "Espy" use strict; use File::Temp qw(tempfile ); #tempdir ); &WERX(); &WERX2(); &DUNWERK(); &DUNWERK2(); ################################################################## sub DUNWERK2 { my $template = 'dunwerk2XXXXXXXXXX'; my $dir = '/tmp/'; my $path = '/tmp/'; (my $fh, my$filename) = tempfile($template, DIR => $dir); print "\nCreated filehandle $fh with filename $filename.\n\n"; print "Press (almost) any key to unlink.\n"; my $continue = (<STDIN>); unlink0 ($fh, $path) or die " num4: Error unlinking file $path sa +fely.\n"; } ################################################################## sub DUNWERK { my $template = 'dunwerkXXXXXXXXXX'; my $dir = '/tmp/'; my $fh = tempfile($template, DIR => $dir); print "\nCreated filehandle $fh.\n"; print "Press (almost) any key to continue\n"; my $continue = (<STDIN>); } ################################################################## sub WERX2 { my $template = 'werx2XXXXXXXXXX'; my $dir = '/tmp/'; (my $fh, my$filename) = tempfile($template, DIR => $dir); print "\nCreated filehandle $fh with filename $filename.\n\n"; print "Press (almost) any key to continue\n"; my $continue = (<STDIN>); } ################################################################## sub WERX { (my $fh, my $filename) = tempfile(); print "\nCreated filehandle $fh with filename $filename.\n"; print "Press (almost) any key to continue\n"; my $continue = (<STDIN>); } ##################################################################

Replies are listed 'Best First'.
(code) Re: can create with File::Temp but not unlink (close enough 8^)
by ybiC (Prior) on Jan 19, 2001 at 18:56 UTC
    Good monk ChOas showed me that I needed to import "unlink0".   And I replaced $path in DUNWERK2 with $filehandle and renamed function to NOWITWERX.   Also fixed typo by replacing my$filename with my $filename in 2 places.  

    That seems to fix everything, except DUNWERK still doesn't appear to create tempfile.   Since I can now create and unlink with NOWITWORKS syntax, this is good enough for now.   Thanks and ++ to ChOas.   8^)

    Updated codelet below.

    #!/usr/bin/perl -w # filetemptest.pl # 2001-01-19 # 2001-01-18 # File::Temp 0.11 Perl 5.00503 # File::Spec 0.82 Debian 2.2 "Espy" use strict; use File::Temp qw(tempfile unlink0); &WERX(); &WERX2(); &DUNWERK(); &NOWITWERX(); ################################################################## sub NOWITWERX { my $template = 'nowitwerxXXXXXXXXXX'; my $dir = '/tmp/'; (my $fh, my $filename) = tempfile($template, DIR => $dir); print "\nCreated filehandle $fh with filename $filename.\n\n"; print "Press (almost) any key to unlink.\n"; my $continue = (<STDIN>); unlink0 ($fh, $filename) or die " NOWITWERX: Error unlinking file + $filename safely.\n"; } ################################################################## sub DUNWERK { my $template = 'dunwerkXXXXXXXXXX'; my $dir = '/tmp/'; my $fh = tempfile($template, DIR => $dir); print "\nCreated filehandle $fh.\n"; print "Press (almost) any key to continue\n"; my $continue = (<STDIN>); } ################################################################## sub WERX2 { my $template = 'werx2XXXXXXXXXX'; my $dir = '/tmp/'; (my $fh, my $filename) = tempfile($template, DIR => $dir); print "\nCreated filehandle $fh with filename $filename.\n\n"; print "Press (almost) any key to continue\n"; my $continue = (<STDIN>); } ################################################################## sub WERX { (my $fh, my $filename) = tempfile(); print "\nCreated filehandle $fh with filename $filename.\n"; print "Press (almost) any key to continue\n"; my $continue = (<STDIN>); } ##################################################################
      I can't get File-Temp to install via PPM (its there but ...) but the readme cautions about permissions on the dir. However, DUNWERK uses:
      my $fh = tempfile($template, DIR => $dir);
      and the rest use:
      (my $fh, my $filename) = tempfile($template, DIR => $dir);
      so ... array returns in scalar context or something?

      a

Re: can create with File::Temp but not unlink
by ChOas (Curate) on Jan 19, 2001 at 17:11 UTC
    Hi!

    Eeeeehm.. Use 'unlink' instead of 'unlink0' ?

    Ahem, my bad... didn't know much about the package ;))

    How about importing unlink0 ? : use File::Temp qw(tempfile unlink0);

    GreetZ!,
      ChOas

    print "profeth still\n" if /bird|devil/;
      Thanks for the quick answer, ChaOs.   8^)
      But attempting to import unlink via use vars qw(); results in the following error:   8^(

      "unlink" is not exported by File::Temp at filetemptest.pl line n
      Compilation aborted.

          cheers,
          Don
          striving for Perl Adept
          (it's pronounced "why-bick")

        Hi,

        Eeehm unlink is exported by main, I think,
        but try and import unlink0 from File::Temp
        Or try to import just everything exportable
        Without specifying the functions

        GreetZ!,
          ChOas

        print "profeth still\n" if /bird|devil/;