On Sun, Mar 4, 2012 at 2:22 AM, Shlomi Fish <shlo...@shlomifish.org> wrote: > Hi lina, > > On Sun, 4 Mar 2012 00:37:58 +0800 > lina <lina.lastn...@gmail.com> wrote: > >> Hi, >> >> I want to output the result into the file shared the same basename but >> different extensions, >> >> Below is what I have come up so far: >> >> perl try.tex >> >> #!/usr/bin/env perl >> >> use strict; >> use warnings; >> use File::Basename; >> > > That's good so far. > >> >> my $bib_filename = "/home/lina/texmf/bibtex/bib/biophymd.bib"; >> my $bib_abbrev_filename ="/home/lina/texmf/bibtex/bib/biophyabbrev.bib" >> > > You're missing a trailing semicolon (";") here. > >> open my $bib_abbrev, '<', $bib_abbrev_filename >> > > And here. > >> my $bib_output, '>', "basename($ARGV[0]).bib" >> > > Perl won't interpolate function calls inside double-quotes. You can do: > > open my $bib_output, '>', basename($filename) . ".bib" > or die "Foo bar. $!"; > > You can also do "@{[basename($filename)]}.bib" (using the "Turtle operator" > mentioned here - http://www.catonmat.net/blog/secret-perl-operators/ - but > this > is clunky. > > You've also missed the open and the trailing ";" again. > >> ### here the ARGV[0] is try.tex, so the output filename is try.bib. >> >> my %dict; >> >> >> my $tex_filename = $ARGV[0] ; >> > > You're mentioning $ARGV[0] twice in this script. You should assign it to a > variable once and use it instead. Also see: > > http://perl-begin.org/tutorials/bad-elements/#subroutine-arguments > >> open my $file, '<', $tex_filename or die "Can't open $tex_filename:$!"; >> > > It is possible that here ":" will be considered as part of the variable name, > because Perl uses "::" as a namespace separator. So you should write > ${tex_filename} instead to delimit it. > > I also dislike calling variables "file" because it can refer to a file handle, > a file name, the file contents or whatever. > >> while (my $line = <$file>) { >> if($line =~ m/cite\{(\S+)\}/g) { >> print $1,"\n"; > > Here it is better to do if (my ($match_substring) =~ m/cite\{(\S+)\}/)) > because > $1 can easily be clobbered (see Perl Best Practices about it). Why are you > using /g here?
It's global match. $ more try.tex bbbbb aaaa \cite{metabolism} ccc \cite{toxic2} EEE \cite{toxic4} ddd cite not $ perl extract.pl try.tex Useless use of a constant (>) in void context at extract.pl line 14. Use of uninitialized value $match_substr in pattern match (m//) at extract.pl line 24, <$texfile> line 1. Use of uninitialized value $match_substr in pattern match (m//) at extract.pl line 24, <$texfile> line 2. Use of uninitialized value $match_substr in pattern match (m//) at extract.pl line 24, <$texfile> line 3. Use of uninitialized value $match_substr in pattern match (m//) at extract.pl line 24, <$texfile> line 4. Use of uninitialized value $match_substr in pattern match (m//) at extract.pl line 24, <$texfile> line 5. #!/usr/bin/env perl use strict; use warnings; use File::Basename; my $INPUTFILE = $ARGV[0]; my $bib_filename = "/home/lina/texmf/bibtex/bib/biophymd.bib"; my $bib_abbrev_filename ="/home/lina/texmf/bibtex/bib/biophyabbrev.bib"; open my $bib_abbrev, '<', $bib_abbrev_filename ; my $bib_output, '>', basename($INPUTFILE)."bib" or die "Can't write the output$!"; my %dict; my $tex_filename = $INPUTFILE ; open my $texfile, '<', $tex_filename or die "Can't open ${tex_filename}:$!"; while (my $match_substr = <$texfile>) { if( my ($match_substr) =~ m/cite\{(\S+)\}/g) { print $match_substr,"\n"; } } Thanks for your help, Best regards, > > Regards, > > Shlomi Fish > > > -- > ----------------------------------------------------------------- > Shlomi Fish http://www.shlomifish.org/ > Best Introductory Programming Language - http://shlom.in/intro-lang > > Beliefs are what divide people. Doubt unites them. > — http://en.wikiquote.org/wiki/Peter_Ustinov > > Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/