in reply to Re^4: search and replace strings in different files in a directory (Path::Tiny)
in thread search and replace strings in different files in a directory

Would you mind providing some explanatory sentences and establish a link to my recent problem?

Sure, see Re: A Beginner Needs Homework help and Re^3: A Beginner Needs Homework help

perlintro#Writing subroutines and http://learn.perl.org/books/beginning-perl/ Chapter 8: Subroutines

  • Comment on Re^5: search and replace strings in different files in a directory (Path::Tiny)

Replies are listed 'Best First'.
Re^6: search and replace strings in different files in a directory (Path::Tiny)
by PitifulProgrammer (Acolyte) on Aug 20, 2014 at 08:57 UTC

    Hi there

    Thanks a mil for providing the links and topics. That was exactly what I was looking for. Like I said, I have not yet touched the subroutines topic in the books I have.

    I will read the relevant chapters and write lines explaining the code that was provided here. I will also post the results to get your opinion on my findings. It might take a while though...

    Thanks a mil for sharing the links.

    Kind regards and take care

    C.

      Dear Monks,

      Thanks you for the exicting read. As promised, I did my homework and tried to comment the code you provided.

      Please find my endeavours below.

      Looking forward to your replies and comments, I am sure I lack some basic understanding and proper perl terminology

      Thanks a mil in advance for your support.

      #!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; # Creates a shortcut for calling Data::Dumper, writing dd is enough to + call module Main( @ARGV ); # main function, takes every file (as an argument) that is passed to @ +ARGV, might be command line or files #that are read in exit( 0 ); #not really a clue, maybe main exits if no arguments are passed to it #This defines the subrotine Main which is called in line 6 (in my edit +or at least) sub Main { dd( -argv, \@ARGV ); #calls Data::Dumper via reference => possibly to print which files are + being passed as arguments (control function) for my $ar ( @ARGV ){ #a for loop to treat each file that has been passed as arg +ument to @ARGV with the sub Sky # each individual file assigned to $ar is in turn passed a +s a parameter to the subroutine Sky( $ar ); } Sky(6); #not really a clue => Does that mean that the sub stops after six runs + } #defining subroutine Sky sub Sky { my( $ra ) = @_; # variable for the argument(s) passed to the sub, @_ accepts every + argument passed to sub, i.e. the files passed to main dd( -ra, $ra ); #print data strucutre of the files -parameter -ra dd( -args, \@_ ); #print data strucutre of the arguments passed to the subroutine } __END__ $ perl sky ("-argv", []) ("-ra", 6) ("-args", [6]) # explains what sky does $ perl sky a b c #if I were to pass a, b, c ("-argv", ["a", "b", "c"]) #@ARGV would contain ["a", "b", "c"] #Lists individual files as they are passed to both the sub Sky and sub + Main ("-ra", "a") ("-args", ["a"]) ("-ra", "b") ("-args", ["b"]) ("-ra", "c") ("-args", ["c"]) #Still no clue about the number, sorrry ("-ra", 6) ("-args", [6])

        Hello PitifulProgrammer,

        Here’s some feedback on your annotations:

        use Data::Dump qw/ dd /; # Creates a shortcut for calling Data::Dumper, writing dd is enough to + call module

        Say rather, writing dd is now enough to call Data::Dump::dd. But actually qw/ dd / is not needed here, because the Data::Dump module exports dd by default.

        Main( @ARGV ); # main function, takes every file (as an argument) that is passed to @ +ARGV, might be command line or files #that are read in

        Correct, but note that in Perl, unlike in C, you don’t need a main function. (I assume that by “command line” you mean command line options.)

        exit( 0 ); #not really a clue, maybe main exits if no arguments are passed to it

        No, exit terminates the script and returns its argument to the shell. By convention, an exit value of zero means “success: the script terminated without errors.”

        dd( -argv, \@ARGV ); #calls Data::Dumper via reference => possibly to print which files are + being passed as arguments (control function)

        No, dd is a Data::Dump function; Data::Dumper is a different module.

        Sky(6); #not really a clue => Does that mean that the sub stops after six runs

        No, it’s just another call to sub Sky with the literal 6 passed as an argument instead of the variable $ar.

        my( $ra ) = @_; # variable for the argument(s) passed to the sub, @_ accepts every + argument passed to sub, i.e. the files passed to main

        Yes, but note that only the first of these arguments is being copied to the scalar variable $ra. (All the arguments passed to the sub remain in the array @_.)

        Hope that helps,

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