Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Not able to execute perl sub routine

by sachin raj aryan (Acolyte)
on Dec 03, 2016 at 06:38 UTC ( [id://1177122]=perlquestion: print w/replies, xml ) Need Help??

sachin raj aryan has asked for the wisdom of the Perl Monks concerning the following question:

I have two files one unzipping.pl and another Main_Start_File.pl. I am calling unzipping.pl from Main file ... problem is that when i uncomment the commented part of Main file it is not executing unzipping.pl whereas when i run it with commented part it is going to unzipping and unzipping all file below is my code

main_file.pl #!/usr/bin/perl use strict; use warnings; use Net::FTP::Recursive ; require "unzipping.pl"; my $host = "xx.xx.xx.xx"; my $user = "tt"; my $password = "tt"; my $basedir = "locations"; my$reportfldr = "reports"; my $filename = 'List.txt'; my $date = "20161128"; #my $brchid="b00330"; ############Fetching report from Server#################### open(my $fh, '<:encoding(UTF-8)', $filename)or die "Could not open fi +le '$filename' $!"; while ( my $brchid = <$fh>) { chomp $brchid; print "$brchid\n"; # system "mkdir $brchid"; # chdir($brchid); # system "mkdir $date"; # chdir ($date); # my $dir = join"/",$basedir,$brchid,$reportfldr,$date; # my $f = Net::FTP::Recursive->new($host,Debug => 0) or die "Can't + open $host\n"; # $f->login($user, $password) or die "Can't log $user in\n"; # $f->binary(); # $f->cwd($dir) or die "Can't cwd to $dir\n"; # $f->dir(); # $f->rget(); # $f->quit; # print " I am entering into subroutine \n"; &unzipping($brchid,$date); # sleep 5; # chdir "../.."; } unzipping.pl #!/usr/bin/perl use strict ; use warnings ; use IO::Uncompress::Gunzip qw(gunzip $GunzipError); sub unzipping{ my $brchdir = $_[0]; my $date = $_[1]; my $dir1 = "Transaction_Reports"; my $dir2 = "Reports_On_Request"; my $dir3 = "Other_Reports"; my $dir4 = "MIS_Reports"; my $dir5 = "Letters_Reminders"; my $dir6 = "Exception_Reports"; my $dir7 = "Concurrent_Audit_Reports"; my $dir8 = "Adhoc_Audit_Reports"; my $dir = $brchdir."/".$date; print "I m in unzipp dir $dir \n"; my $dirf=$dir."/".$dir1; my $dirs = $dir."/".$dir2; my $dirt = $dir."/".$dir3; my $dirft = $dir."/".$dir4; my $dirfv = $dir."/".$dir5; my $dirsx = $dir."/".$dir6; my $dirsn = $dir."/".$dir7; my $direi = $dir."/".$dir8; print "I m here \n" ; print " $dir"; for my $input ( glob "$dir/*.gz" ) { print " I m inside for $dir \n"; my $output = $input; $output =~ s/.gz// ; gunzip $input => $output or die "Error Uncompressing '$input': $GunzipError\n"; } } 1;

Replies are listed 'Best First'.
Re: Not able to execute perl subroutine
by Athanasius (Archbishop) on Dec 03, 2016 at 07:16 UTC

    Hello sachin raj aryan, and welcome to the Monastery!

    In the commented-out code, you have chdir($brchid); and chdir($date);, but you don’t change back to the original directory before calling the unzipping() function. My guess is that the unzipping function is unable to find the directory named by $brchid from within the current working directory (viz., $brchid/$date), so it does nothing. If you change the working directory back before calling unzipping(), it should work as it does when the code is commented out.

    BTW, in modern Perl it’s no longer a good idea to call functions with a leading ampersand — that circumvents prototypes (see perlsub). Just call the function like this:

    unzipping($brchid, $date);

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Not able to execute perl sub routine
by Laurent_R (Canon) on Dec 03, 2016 at 10:24 UTC
    Assuming that Athanasius's comment will solve your issue, I would just further comment that you should probably consider using one or two array(s) of directories rather than having all these $dir1, $dir2, ..., $dirs, $dirt, etc. variables.

    And of course, I agree that you should most probably avoid the function call with a leading ampersand.

Re: Not able to execute perl sub routine
by Marshall (Canon) on Dec 03, 2016 at 21:04 UTC
    Hi Sachin! It is certainly possible to split the "main" package across several .pl files. However I would not recommend that approach. I would put your unzipping sub in a module. You can look at Module Tutorials for a lot of info about making modules.

    To get you started, here is something that you can just cut-n-paste your code into.

    #!/usr/bin/perl # This is a main program... use strict; use warnings; use ZipUtilDemo; #unzipping imported by default here print "starting main program\n"; unzipping(); #call unzipping sub in module ZipUtilDemo.pm __END__ Prints: starting main program Sub unzipping called!
    The module:
    #!/usr/bin/perl #This is file: ZipUtilDemo.pm package ZipUtilDemo; use strict; use warnings; use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION); use Exporter; our $VERSION=1.00; our @ISA = qw(Exporter); our @EXPORT = qw( unzipping ); our @EXPORT_OK = qw( ); sub unzipping { print "Sub unzipping called!\n"; #of course real code goes here. } 1; #IMPORTANT return a true value!
Re: Not able to execute perl sub routine
by Discipulus (Canon) on Dec 03, 2016 at 16:20 UTC
    welcome to the monastery sachin raj aryan!

    if &sub is looking old style to many monks (not me, and is quite good if you do not use prototypes at all and if if you are aware that &sub; will call &sub(@_); behind the scenes), what is looking very weird is the ancient require "file.pl"; style.

    Code to be required and it's contained subs must go in modules and modules must be used via use

    A simple module is more flexible than a required file, is easier to be tested.

    Perl Modules are described in perlmod

    PS: many simple module implementations can profit of the Exporter one to pull subroutines into the namespace.

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      if &sub is looking old style to many monks (not me, and is quite good if you do not use prototypes at all and if if you are aware that ⊂ will call &sub(@_); behind the scenes)
      I disagree, it is not "quite good". Or I would at least phrase it the other way around: It is really bad unless you really know what you're doing and really need that feature. Otherwise, simply don't use that obsolete syntax.

      For the rest, I agree that it is usually better to use a module with use rather than require.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1177122]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-19 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found