need a find/replace command to fix my require_once

need a find/replace command to fix my require_once

am 20.11.2009 02:43:44 von Daevid Vincent

I was reading this: http://pear.php.net/manual/en/standards.including.php
and it states:

"Note: include_once and require_once are statements, not functions.
Parentheses should not surround the subject filename."

I never knew that. I've always (wrongly) used:

require_once('/path/to/my/file');

Anyone have a bash command line snippet (or other code is fine too I guess)
that will fix all my directory tree?

The tricks are that I think there can be several variations and several
instances with a given file too:

require_once('/path/to/my/file');
require_once ('/path/to/my/file');
require_once("/path/to/my/file");
require_once(ROOTPATH."/my/file");

etc. Note the space before the parens, the single vs. double quotes and the
use of a global define.

I think this regex should work:

require_once\s?\((.*?)\);

I just don't know the magic sed/awk/grep/whatever to do the search and
replace on all my .php files recursively.

This seems like a useful 'routine' to post up on that page, as I'm sure I'm
not the only one who didn't know this little subtlety (considering PHP
doesn't puke on it either).


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: need a find/replace command to fix my require_once

am 20.11.2009 10:20:31 von Ashley Sheridan

--=-YLKAdX04YWI2qQxa9+hE
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

On Thu, 2009-11-19 at 17:43 -0800, Daevid Vincent wrote:

> I was reading this: http://pear.php.net/manual/en/standards.including.php
> and it states:
>
> "Note: include_once and require_once are statements, not functions.
> Parentheses should not surround the subject filename."
>
> I never knew that. I've always (wrongly) used:
>
> require_once('/path/to/my/file');
>
> Anyone have a bash command line snippet (or other code is fine too I guess)
> that will fix all my directory tree?
>
> The tricks are that I think there can be several variations and several
> instances with a given file too:
>
> require_once('/path/to/my/file');
> require_once ('/path/to/my/file');
> require_once("/path/to/my/file");
> require_once(ROOTPATH."/my/file");
>
> etc. Note the space before the parens, the single vs. double quotes and the
> use of a global define.
>
> I think this regex should work:
>
> require_once\s?\((.*?)\);
>
> I just don't know the magic sed/awk/grep/whatever to do the search and
> replace on all my .php files recursively.
>
> This seems like a useful 'routine' to post up on that page, as I'm sure I'm
> not the only one who didn't know this little subtlety (considering PHP
> doesn't puke on it either).
>
>

You could try something like this:

find . -maxdepth 1 -name "*.php" -type f -exec sed -i 's/find
text/replace text/' {} \;

Note that the find and replace strings are regular expressions,
separated by the / character here, so ()'s will need to be escaped as
they have a special meaning in regular expressions. The maxdepth
parameter of find can be set to higher than 1 to let the script work
recursively on files within directories.

Thanks,
Ash
http://www.ashleysheridan.co.uk



--=-YLKAdX04YWI2qQxa9+hE--

Re: need a find/replace command to fix my require_once

am 20.11.2009 10:28:48 von Nathan Rixham

Daevid Vincent wrote:
> I was reading this: http://pear.php.net/manual/en/standards.including.php
> and it states:
>
> "Note: include_once and require_once are statements, not functions.
> Parentheses should not surround the subject filename."
>
> I never knew that. I've always (wrongly) used:
>
> require_once('/path/to/my/file');
>

if it ain't broken don't fix it - why not just ensure you do it the
correct way (or preferred way) in the future.

sure the time could better be invested fixing a bug, writing a test or
documenting something.

s'all your call though :)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Re: need a find/replace command to fix my require_once

am 20.11.2009 16:42:29 von Shawn McKenzie

Nathan Rixham wrote:
> Daevid Vincent wrote:
>> I was reading this: http://pear.php.net/manual/en/standards.including.php
>> and it states:
>>
>> "Note: include_once and require_once are statements, not functions.
>> Parentheses should not surround the subject filename."
>>
>> I never knew that. I've always (wrongly) used:
>>
>> require_once('/path/to/my/file');
>>
>
> if it ain't broken don't fix it - why not just ensure you do it the
> correct way (or preferred way) in the future.
>
> sure the time could better be invested fixing a bug, writing a test or
> documenting something.
>
> s'all your call though :)

I agree. That's a PEAR coding standard, so unless you're coding a PEAR
package it doesn't matter. It's the PEAR groups opinion. If you look
at some other project, they may require the parentheses for readability.

--
Thanks!
-Shawn
http://www.spidean.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Re: need a find/replace command to fix my require_once [solved]

am 21.11.2009 01:31:29 von Daevid Vincent

> -----Original Message-----
> From: Shawn McKenzie [mailto:nospam@mckenzies.net]
> Sent: Friday, November 20, 2009 7:42 AM
> To: Nathan Rixham
> Cc: Daevid Vincent; php-general@lists.php.net
> Subject: [PHP] Re: need a find/replace command to fix my require_once
>
> Nathan Rixham wrote:
> > Daevid Vincent wrote:
> >> I was reading this:
> http://pear.php.net/manual/en/standards.including.php
> >> and it states:
> >>
> >> "Note: include_once and require_once are statements,
> not functions.
> >> Parentheses should not surround the subject filename."
> >>
> >> I never knew that. I've always (wrongly) used:
> >>
> >> require_once('/path/to/my/file');
> >>
> >
> > if it ain't broken don't fix it - why not just ensure you do it the
> > correct way (or preferred way) in the future.
> >
> > sure the time could better be invested fixing a bug,
> writing a test or
> > documenting something.
> >
> > s'all your call though :)
>
> I agree. That's a PEAR coding standard, so unless you're
> coding a PEAR
> package it doesn't matter. It's the PEAR groups opinion. If you look
> at some other project, they may require the parentheses for
> readability.

Well now, I am confused because these pages:
http://us2.php.net/manual/en/function.include.php
http://us2.php.net/manual/en/function.require.php
http://us2.php.net/manual/en/function.require-once.php
http://us2.php.net/manual/en/function.include-once.php

All show them as functions:
Include(), require(), require_once(), include_once()

Yet ALL of their examples show the PEAR way:
http://pear.php.net/manual/en/standards.including.php

include_once "a.php";

I opted to just do it anyways since it seems that is the "official" way.

To change all require_once('foo.php'); to require_once 'foo.php' execute this:

cd /var/www/

find . -name '*.php' -print | xargs egrep -l \
'require_once\s*(\(.*\));'\ | xargs sed -i.sedorig -e \
's/require_once\s*(\(.*\));/require_once \1;/'

(thanks to Robert Hajime Lanning for that)

Then to remove all the ".php.sedorig" backup files execute this:

find . -name "*.php.sedorig" -type f -exec rm -rf {} \;

I tried to post this note to here:
http://us2.php.net/manual/add-note.php
But the stupid-ass form says:
"Your note contains a prohibited (usually SPAM) word. Please remove it and try again."
WTF!? It doesn't even TELL me what f'n word is the problem. Seriously? It already asked me the stupid math question and now this
B.S.?



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: Re: need a find/replace command to fix my require_once [webform solved]

am 21.11.2009 01:51:34 von Daevid Vincent

> I tried to post this note to here:
> http://us2.php.net/manual/add-note.php
> But the stupid-ass form says:
> "Your note contains a prohibited (usually SPAM) word. Please
> remove it and try again."
> WTF!? It doesn't even TELL me what f'n word is the problem.
> Seriously? It already asked me the stoooopid math question and now this B.S.?

I have to laugh because I removed the text:

http://php.net/manual/en/function.include.php
http://php.net/manual/en/function.require.php
http://php.net/manual/en/function.require-once.php
http://php.net/manual/en/function.include-once.php

And the submit note worked. LOL. So apparently one of those words in there is considered SPAM by PHP's own web form. Ha! Suckas.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

RE: need a find/replace command to fix my require_once

am 21.11.2009 02:13:52 von Daevid Vincent

> -----Original Message-----
> From: Nathan Rixham [mailto:nrixham@gmail.com]
> Sent: Friday, November 20, 2009 1:29 AM
> To: Daevid Vincent
> Cc: php-general@lists.php.net
> Subject: Re: need a find/replace command to fix my require_once
>
> Daevid Vincent wrote:
> > I was reading this:
> http://pear.php.net/manual/en/standards.including.php
> > and it states:
> >
> > "Note: include_once and require_once are statements,
> not functions.
> > Parentheses should not surround the subject filename."
> >
> > I never knew that. I've always (wrongly) used:
> >
> > require_once('/path/to/my/file');
> >
>
> if it ain't broken don't fix it - why not just ensure you do it the
> correct way (or preferred way) in the future.
>
> sure the time could better be invested fixing a bug, writing a test or
> documenting something.

I considered this of course. But...

[a] Other coders will no doubt take a completed page as a template and modify, thereby causing MORE erroneous syntactical code.
[b] I can't think of any other language that uses parenthesis for an 'include' (C, Perl, Java, Ruby, etc.) with the exception of
maybe LOLcode.
[c] It does seem to be the PHP blessed way (PEAR or not) looking at their examples even.
[d] it was a good exercise in learning more about 'sed', 'find' and bash pipes. Not that I wrote it all, but deciphering what others
showed me really helped.
[e] because I'm a guy and a coder, and that's what we do. We do stuff 'just cause' and we 'refactor' useless shit for our own
satisfaction. ;-)


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php