On Tue, 3 Sep 2002, ANIDIL RAJENDRAN wrote: > Newbie again. > Sorry if this question sounds so silly > > Want to open filenames like ~username/blah but open doesn't interpret the tilde to >mean the > home directory > > Solution > > expand the filename manually with substitution > > $filename =~ s{ ^ ~ ( [^/]* ) } > { $1 > ? (getpwnam($1)) [7] > : ( $ENV{HOME} || $ENV{LOGDIR} > || (getpwuid($>)) [7] > ) > }ex; > > The auther says the following can me matched > ~user > ~user/blah > ~ > ~/blah > > Could someone make me clear how these can be matched by the pattern inside the >parenthesis > which is ( [^/]*) >
First up the two modifiers 'e' and 'x' used at the end. (perldoc perlre) The e modifier specifies that the replacement part of the substitution is to evaluated as perl statements and the result used for the substitution. The x modifier tells the regex engine to ignore whitespaces and the '#' character. This allows you to write a readable pattern match with comments. The search part of the substitution { ^ ~ ( [^/]* ) } Note with /x modifier on, the white spaces will be ignored. This says look for '~' at the start of $filename followed 0 or more non '/' characters which will be saved in $1. The replacement part { $1 ? (getpwnam($1)) [7] : ( $ENV{HOME} || $ENV{LOGDIR} || (getpwuid($>)) [7] ) }ex; Read through these docs, (perldoc -f getpwnam) and (perldoc -f getpwuid). This uses the ternary operator, if $1 is true the statements after '? and before ':' will be evaluated. If it is false that after ':' will be evaluated. (perldoc perlop) When $filename is '~' $1 contains nothing i.e. evaluates to false. This means '~' is substituted by the result of the statements after ':'. When $filename is '~user' $1 contains 'user', this means '~user' is substituted by the results of (getpwnam($1))[7]. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]