Grabbing Text from file and assigning them to variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • want2learnperl
    New Member
    • Aug 2007
    • 1

    Grabbing Text from file and assigning them to variables

    Perl Newbie here.

    I want to open a file that I have setup as a configuration file. After opening the file I was to assign parts of the words to variables.

    for example (configuration file config.conf):

    Code:
    cake=yellow,chocolate,red,white
    icing=butter,lemon,chocolate
    I want to open the above file, config.conf, and read it while assiging variables to the items after the equals sign.

    [CODE=perl]
    my($cake,$icing );

    $cake = undef;
    $icing = undef;

    open(handle,"co nfig.conf") || die "no file found\n";
    foreach $line (<handle>) {
    my( $cake) = $line =~ /^cake=(.*)/;
    my($icing) = $line =~ /^icing=(.*)/;
    }
    print $cake; #nothing prints
    print $icing; #nothing prints
    close(handle);

    #next i would like to split the $cake items by using the split command
    #then I would split the $icing items
    #tricky part I think is trying to create a directory structure where each cake has
    # below it a sub directory for each icing
    #I plan on populating taste results under the sub directory
    #ex:
    [/CODE]
    • /yellow
      • /butter
      • /lemon
      • /chocolate

    • /white
      • /butter
      • /lemon
      • /chocolate


    $cake and $icing do not print.

    If someone could help with my logic I would appreciate the hlep.
    Last edited by miller; Aug 29 '07, 11:09 PM. Reason: Code Tag and ReFormatting
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    you have a scoping problem:

    my($cake,$icing );#<-- here you declared with "my"

    $cake = undef;
    $icing = undef;

    open(FH,"config .conf") || die "no file found\n";
    while (my $line = <FH>) {
    my( $cake) = $line =~ /^cake=(.*)/;#<-- $cake declared again with "my"
    my($icing) = $line =~ /^icing=(.*)/;#<-- $icing declared again with "my"
    }

    just remove the "my" from the variables in the loop and it will work but it will assign the last match in the file to those variables. If there is only one possible match than you will be OK. Note I changed "foreach" to "while" and "filehandle " to "FH". "foreach" will not process a file properly and filehandles should aways be uppercase if you use barewords like FH or FILEHANDLE. This avoids collisions with functions and other things that are barewords.

    Comment

    • miller
      Recognized Expert Top Contributor
      • Oct 2006
      • 1086

      #3
      Alternatively, you could use a config module like Config::Tiny.

      [CODE=perl]
      use Config::Tiny;

      # Open the config
      $config = Config::Tiny->read( 'config.conf' );

      my $cake = $config->{_}{cake};
      my $icing = $config->{_}{icing};

      print "$cake\n";
      print "$icing\n";
      [/CODE]

      - Miller

      Comment

      Working...